/**
 * 
 */
package com.res.simulator.handler;

import com.res.common.handler.CommonHandler;
import com.res.simulator.model.UserManagementModel;
import com.res.simulator.plugins.MessageCommunicationClientPlugin;
import com.res.simulator.view.UserManagementView;
import com.sip.comserver.communication.CommunicationMessage;
import com.sip.comserver.communication.utils.CommonUtils;

/**
 * @author Dhanushka Ariyasingha
 *
 */
public class UserManagementHandler implements CommonHandler<UserManagementView, UserManagementModel> {

	public UserManagementView process(final UserManagementModel model) throws Exception {
		// TODO Auto-generated method stub
		final UserManagementView view = new UserManagementView();		
		final CommunicationMessage message = extract(model);
		
		final MessageCommunicationClientPlugin plugin = new MessageCommunicationClientPlugin();
		final CommunicationMessage returnedMessage = plugin.invoke(message);
		final String valueString = returnedMessage.getMessageBody();
		
		System.out.println(valueString);
		
		// Updating view
		update(view, valueString);
		
		return view;
	}
	
	private void update(final UserManagementView view,final String valueString) throws Exception {

		view.setUserExternalId(CommonUtils.find(valueString, "USER_ID"));
		
		final String status = convertCustomerStatusById(CommonUtils.find(valueString, "CUSTOMER_STATUS"));
		view.setStatus(status);
		
		final String attributeString = extractOutputString(valueString);
		view.setUserAtributes(attributeString);
		
	}
	
	/**
	 * Value String -
	 *ACCESS_TYPE:Fios|SERVICE_LIST:dsl|STATE:TX|CUSTOMER_TYPE:Consumer|CUSTOMER_STATUS:1
	 * 
	 * @param model
	 * @return
	 * @throws Exception 
	 */
	private CommunicationMessage extract(final UserManagementModel model) throws Exception {
	
		final CommunicationMessage message = new CommunicationMessage();
		String valueString = model.getUserInputString();
		// COMMAND:RECYCLEDUSER
		
		final String mainCommand = model.getCommand();
		final String subCommand = CommonUtils.find(valueString, "COMMAND");
		
		valueString = valueString.replace(subCommand, mainCommand + "-" + subCommand);

		message.setMessageHeader("JOBType:UserManagement|ID:dev44.aepona.com");
		message.setMessageBody(valueString);
		
		return message;
	}
	
	
	private String extractOutputString(final String valueString) throws Exception {

		final StringBuilder builder = new StringBuilder();

		if(valueString.contains("ACCESS_TYPE:")){
			builder.append("ACCESS_TYPE:" + CommonUtils.find(valueString, "ACCESS_TYPE") + ",");
		}
		
		if(valueString.contains("SERVICE_LIST:")){
			builder.append("SERVICE_LIST:" + CommonUtils.find(valueString, "SERVICE_LIST") + ",");
		}
		
		if(valueString.contains("STATE:")){
			builder.append("STATE:" + CommonUtils.find(valueString, "STATE") + ",");
		}
		
		builder.append("<br />");
		
		if(valueString.contains("CUSTOMER_TYPE:")){
			builder.append("CUSTOMER_TYPE:" + CommonUtils.find(valueString, "CUSTOMER_TYPE") + ",");
		}
		
		if(valueString.contains("COMMAND:")){
			builder.append("COMMAND:" + CommonUtils.find(valueString, "COMMAND") + ",");
		}

		return builder.toString();

	}
	
	private String convertCustomerStatusById(final String statusValue){
		
		String status = statusValue;
		
		if(statusValue.contains("0")){
			
			status = "PENDING";
			
		} else if(statusValue.contains("1")){
			
			status = "COMPLETE";
			
		}
		
		return status;
	}

}
